package com.project.finance.common.util;

import com.alibaba.fastjson.JSONObject;
import com.mzlion.easyokhttp.HttpClient;
import com.mzlion.easyokhttp.request.PostRequest;

import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @Author: Brendan Li
 * @description 短信工具
 * @Date: 2019/12/09/16:11
 */
public class SMSUtils {
    public static final String APPKEY = "3deb7a6a4fb9450c";
    public static final String URL = "https://api.jisuapi.com/sms/send";

    public static String sendSMS(String mobile) throws Exception {

        String verify_code = "0123456789";
        Random random = new SecureRandom();

        char[] smsCodes = new char[4];
        for (int i = 0; i < smsCodes.length; i++) {
            smsCodes[i] = verify_code.charAt(random.nextInt(verify_code.length()));
        }

        String smsCode = new String(smsCodes);


        String content = "验证码：（@"+smsCode+"），您正在使用短信验证码登陆功能，5分钟内有效。转发可能导致账号被盗，请勿泄露给他人【大向随行】";// utf-8

        String url = URL ;

        Map<String, String> parameters = new HashMap<>();
        parameters.put("content", content);
        parameters.put("mobile",mobile);
        parameters.put("appkey", APPKEY);

        PostRequest request = HttpClient.post(url).param(parameters);

        String result = request.asString();

        JSONObject json = JSONObject.parseObject(result);

        if(json.getInteger("status")!=0){
            return "fail";
        }else {
            return smsCode;
        }
    }
}
